//
//  YZKCheckListView.m
//  GroupContract
//
//  Created by yzk on 14-7-28.
//  Copyright (c) 2014年 www.hawk-data.com. All rights reserved.
//

#import "YZKCheckListView.h"
#import <objc/runtime.h>

static const CGFloat YZKCustomButtonHeight = 30;

@interface YZKCheckListView ()

@property (nonatomic, strong) UIView *maskView;

@property (nonatomic, retain) UITableView *mainCheckListView;                   //主的选择列表视图
@property (nonatomic, retain) UIButton *doneButton;                             //确定选择按钮
@property (nonatomic, retain) UIButton *cancelButton;                           //取消选择按钮

//初始化界面
- (void)initTheInterface;

//动画进入
- (void)animatedIn;

//动画消失
- (void)animatedOut;

@end

@implementation YZKCheckListView



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initTheInterface];
        
        
        _maskView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0;
    }
    return self;
}

- (void)initTheInterface
{
    self.backgroundColor = [UIColor lightGrayColor];
//    self.layer.borderColor = [[UIColor lightGrayColor] CGColor];
//    self.layer.borderWidth = 0.5f;
    self.layer.cornerRadius = 10.0f;
    self.clipsToBounds = TRUE;
    
    _titleName = [[UILabel alloc] initWithFrame:CGRectZero];
    self.titleName.font = [UIFont systemFontOfSize:17.0f];
    self.titleName.backgroundColor = [UIColor colorWithRed:7 / 255.0
                                                     green:125.0 /255.0
                                                      blue:203/ 255.0
                                                     alpha:1];
    
    self.titleName.textAlignment = NSTextAlignmentCenter;
    self.titleName.textColor = [UIColor whiteColor];
    CGFloat xWidth = self.bounds.size.width;
    self.titleName.lineBreakMode = NSLineBreakByTruncatingTail;
    self.titleName.frame = CGRectMake(0, 0, xWidth, 32.0f);
    [self addSubview:self.titleName];
    
    CGRect tableFrame = CGRectMake(0, 32.0f, xWidth, self.bounds.size.height-32.0f-YZKCustomButtonHeight);
    _mainCheckListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    self.mainCheckListView.dataSource = self;
    self.mainCheckListView.delegate = self;
    self.mainCheckListView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mainCheckListView.allowsMultipleSelection = YES;
    self.mainCheckListView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.mainCheckListView];
    
    self.doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.doneButton.frame = CGRectMake(0, self.bounds.size.height - YZKCustomButtonHeight+1, self.bounds.size.width / 2.0f-0.5, YZKCustomButtonHeight);
    self.doneButton.backgroundColor = [UIColor whiteColor];
//    [self.doneButton setBackgroundImage:nil forState:UIControlStateNormal];
    [self.doneButton addTarget:self
                        action:@selector(buttonWasPressed:)
              forControlEvents:UIControlEventTouchUpInside];
    [self.doneButton setTitle:@"确定" forState:UIControlStateNormal];
    [self addSubview:self.doneButton];
    
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self.cancelButton setBackgroundImage:nil forState:UIControlStateNormal];
    self.cancelButton.frame = CGRectMake(self.bounds.size.width / 2.0f+0.5, self.bounds.size.height - YZKCustomButtonHeight+1, self.bounds.size.width / 2.0f-0.5, YZKCustomButtonHeight);
    self.cancelButton.backgroundColor = [UIColor whiteColor];
    [self.cancelButton addTarget:self
                          action:@selector(buttonWasPressed:)
                forControlEvents:UIControlEventTouchUpInside];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [self addSubview:self.cancelButton];
    
}

- (NSArray *)indexPathsForSelectedRows
{
    return [self.mainCheckListView indexPathsForSelectedRows];
}

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.mainCheckListView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

#pragma mark - Reuse Cycle
- (id)dequeueReusableCheckListCellWithIdentifier:(NSString *)identifier
{
    return [self.mainCheckListView dequeueReusableCellWithIdentifier:identifier];
}

- (UITableViewCell *)checkListCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.mainCheckListView cellForRowAtIndexPath:indexPath];
}

#pragma mark - UITableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.datasource && [self.datasource respondsToSelector:@selector(checkListView:numberOfRowsInSection:)])
    {
        return [self.datasource checkListView:self numberOfRowsInSection:section];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.datasource && [self.datasource respondsToSelector:@selector(checkListView:cellForRowAtIndexPath:)])
    {
        return [self.datasource checkListView:self cellForRowAtIndexPath:indexPath];
    }
    return nil;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkListView:didDeselectRowAtIndexPath:)])
    {
        [self.delegate checkListView:self didDeselectRowAtIndexPath:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkListView:didSelectRowAtIndexPath:)])
    {
        [self.delegate checkListView:self didSelectRowAtIndexPath:indexPath];
    }
}

#pragma mark - Animated Mthod
- (void)animatedIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)animatedOut
{
    self.alpha = 1;
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

#pragma mark - show or hide self
- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    
    [keywindow addSubview:_maskView];
    [UIView animateWithDuration:.35
                     animations:^{
                         _maskView.alpha = 0.5;
                     }
                     completion:^(BOOL finished){
                     }];
    
    [keywindow addSubview:self];
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,
                              keywindow.bounds.size.height/2.0f);
    
    [self animatedIn];
}

- (void)dismiss
{
    [UIView animateWithDuration:.05
                     animations:^{
                         _maskView.alpha = 0;
                     }
                     completion:^(BOOL finished){
                         [_maskView removeFromSuperview];
                     }];
    
    [self animatedOut];
}


#pragma mark - UIButton Clicke Method
- (void)buttonWasPressed:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if (button == self.cancelButton)
    {
        if (self.delegate && [self.delegate respondsToSelector:@selector(checkListViewDidClickForCancel:)]) {
            [self.delegate checkListViewDidClickForCancel:self];
        }
    }
    else
    {
        if (self.delegate && [self.delegate respondsToSelector:@selector(checkListViewDidClickForDone:)]) {
            [self.delegate checkListViewDidClickForDone:self];
        }
    }
    [self animatedOut];
}

@end

@implementation YZKCheckListViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.font = [UIFont systemFontOfSize:14];
        
        _checkImageView          = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]];
        _checkImageView.frame    = CGRectMake(155, 10, 25, 25);
        [self.contentView addSubview:_checkImageView];
        _checkImageView.hidden   = YES;

        UIView *lineView         = [[UIView alloc] initWithFrame:CGRectMake(5, 43, 190, 1)];
        lineView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
        [self.contentView addSubview:lineView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    _checkImageView.hidden = !selected;
}

@end
